package com.itheima.service;

import com.itheima.api.SmsHomeBrandApi;
import com.itheima.exception.ConsumerException;
import com.itheima.pojo.SmsHomeBrand;
import com.itheima.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author pk
 * @date 2022/10/13/15:52
 */
@Service
public class SmsHomeBrandService {

    @DubboReference
    private SmsHomeBrandApi brandApi;

    /**
     * 添加首页推荐品牌
     * @param brand
     */
    public void createBrand(SmsHomeBrand brand) {
        brandApi.createBrand(brand);
    }

    /**
     * 分页查询品牌推荐
     * @param brandName
     * @param pageNum
     * @param pageSize
     * @param recommendStatus
     * @return
     */
    public PageResult findBrands(String brandName, Integer pageNum, Integer pageSize, Integer recommendStatus) {

        return brandApi.findBrands(brandName,pageNum,pageSize,recommendStatus);
    }

    /**
     * 修改品牌排序
     * @param id
     * @param sort
     */
    public void updateSortByid(Long id, Integer sort) {
        //根据id查询品牌实例
        SmsHomeBrand brand=brandApi.findBrandById(id);
        if (brand!=null){
            if (sort!=null){
                brand.setSort(sort);
            }
        }
        //调用api修改
        brandApi.updateSortByid(brand);
    }

    /**
     * 批量删除功能
     * @param id
     */
    public void deleteBrands(Integer[] id) {
        List<Long> ids = Arrays.stream(id).map(e->Long.valueOf(e)).collect(Collectors.toList());
        //调用api删除
        if (!brandApi.deleteBrands(ids)){
            //删除失败则抛出异常
            throw new ConsumerException("网络繁忙请刷新重试");
        }
    }

    /**
     * 批量修改品牌状态
     * @param id
     * @param recommendStatus
     */
    public void updateRecommendStatus(Integer[] id, Integer recommendStatus) {
        List<Long> ids = Arrays.stream(id).map(e->Long.valueOf(e)).collect(Collectors.toList());
        //调用api修改
        if (!brandApi.updateRecommendStatus(ids,recommendStatus)){
            //修改失败则抛出异常
            throw new ConsumerException("网络繁忙请刷新重试");
        }
    }
}
